<template>
	<div class="p_20 indexBanner">
		<el-dialog :visible.sync="isShow" :close-on-click-modal="false" @close="closeDialog">
			<!-- <el-divider></el-divider> -->
			<el-form :model="form" label-width="80px">
				<el-form-item label="新闻时间">
					<el-date-picker v-model="form.newsdate" type="datetime" value-format="yyyy-MM-dd hh:mm:ss" placeholder="选择日期">
					</el-date-picker>
				</el-form-item>
				<el-row>
					<el-col :span="12">
						<el-form-item label="推荐">
							<el-select v-model="form.recommend">
								<el-option :value="1" label="是"></el-option>
								<el-option :value="0" label="否"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="是否推荐置顶" label-width="120px">
							<el-select v-model="form.top">
								<el-option :value="1" label="是"></el-option>
								<el-option :value="0" label="否"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-form-item label="标题">
					<el-input v-model="form.title" class="w_400"></el-input>
				</el-form-item>

				<UploadImg ref="UploadImg" :upImg="upImg"></UploadImg>
				<UploadImg ref="UploadImg" :upImg="upImg1"></UploadImg>
				<UploadImg ref="UploadImg" :upImg="upImg2"></UploadImg>
				<el-form-item label="内容">
					<el-upload :action="imgBaseURL" class="m_b_20" :on-change="handleAvatarSuccess1" :auto-upload="false" show-file-list>
						<el-button size="small" type="primary">点击上传</el-button>
						<div slot="tip" class="el-upload__tip">仅支持单个文件</div>
					</el-upload>
					<UE ref="UE" :value="form.content"></UE>
				</el-form-item>
				<!-- <el-form-item label="语言">
					<el-input v-model="form.language" class="w_400"></el-input>
					<el-select v-model="form.language">
						<el-option label="cn" value="cn">cn</el-option>
						<el-option label="en" value="en">en</el-option>
					</el-select>
				</el-form-item> -->
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="closeDialog">取 消</el-button>
				<el-button type="primary" @click="submit">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				isShow: false,
				form: {
					newsdate: '',
					recommend: '',
					top: '',
					title: '',
					content: '',
					file: '',
					file1: '',
					file2: '',
					file3: '',
				},

				upImg: {
					label: "列表图片",
					imageUrl: "",
					name: "file",
					width: "390",
					height: "220",
					file: "",
				},
				upImg1: {
					label: "置顶图片",
					imageUrl: "",
					name: "file",
					width: "531",
					height: "424",
					file: "",
				},
				upImg2: {
					label: "首页推图",
					imageUrl: "",
					name: "file",
					width: "416",
					height: "416",
					file: "",
				},
				fileList:[],
				//图片上传
				imageUrl: "",
				// dialogVisible: false,
				// disabled: false
			};
		},
		methods: {
			getDetail(id) {
				this.axiosPost("News/newsShow", {
					id: id
				}, (res) => {
					console.log(res);
					if (res.data.code == 0) {
						this.form = res.data.data[0];
						// delete this.form.newsdate;
						if (this.form.file) {
							this.upImg.imageUrl =
								this.imgURL + this.form.file.url + "/" + this.form.file.name;
						}
						if (this.form.file1) {
							this.upImg1.imageUrl =
								this.imgURL + this.form.file1.url + "/" + this.form.file1.name;
						}
						if (this.form.file2) {
							this.upImg2.imageUrl =
								this.imgURL + this.form.file2.url + "/" + this.form.file2.name;
						}
						if(this.form.file3) {
							// this.fileList.push()
							this.fileList = [{name:this.form.file.name}]
						}
						// console.log(this);
						this.$refs.UE.setContent(this.form.content)
					}
				});
			},

			//提交
			submit() {
				//   let params = this.form;
				//   console.log(params);
				if(!this.form.newsdate) {
					this.msgError("请选择新闻时间 ! ");
					return ;
				}
				
				let params = new FormData();

				params.append('id', this.form.id);
				params.append('top', this.form.top);
				params.append('recommend', this.form.recommend);
				params.append('newsdate', this.form.newsdate);
				params.append('year', this.form.newsdate.substr(0,4));
				params.append('title', this.form.title);
				params.append('content', this.$refs.UE.getUEContent())


				params.append('language', this.$store.getters.getLanguage);

				if (this.upImg.file) {
					params.append('file', this.upImg.file)
				}
				if (this.upImg1.file) {
					params.append('file1', this.upImg1.file)
				}
				if (this.upImg2.file) {
					params.append('file2', this.upImg2.file)
				}
				if(this.fileList.file) {
					// this.fileList.push()
					params.append('file3' , this.fileList[0].file.raw)
				}


				if (this.form.id) {
					this.edit(params);
				} else {
					this.add(params);
				}
			},

			//新增
			add(params) {
				this.axiosPostFile("News/newsAdd", params, (res) => {
					if (res.data.code === 0) {
						this.msgSuccess(res.data.message);
						this.$parent.getTableData();
						this.isShow = false;
					} else {
						this.msgError(res.data.message);
					}
				});
			},

			//编辑
			edit(params) {
				this.axiosPostFile("News/newsEdit", params, (res) => {
					if (res.data.code === 0) {
						this.msgSuccess(res.data.message);
						this.$parent.getTableData();
						this.isShow = false;
					} else {
						this.msgError(res.data.message);
					}
				});
			},

			//图片上传
			handleAvatarSuccess1(file) {
				this.fileList = [{name:file.name , file:file}];
				console.log(this.fileList);
				// this.form.audio = file;
				// this.fileList = [{file:file,name:file.name}]
				// this.form.language = this.id;
				return false;
			},

			//关闭窗口 清空数据
			closeDialog() {
				this.form = {
					banner_id: "",
					banner_title: "",
					image_id: "",
					url: "",
					sort: 0,
					language: "cn",
				};
				this.upImg = {
					label: "列表图片",
					imageUrl: "",
					name: "file",
					width: "390",
					height: "220",
					file: "",
				},
				this.upImg1 = {
					label: "置顶图片",
					imageUrl: "",
					name: "file",
					width: "531",
					height: "424",
					file: "",
				},
				this.upImg2 = {
					label: "首页推图",
					imageUrl: "",
					name: "file",
					width: "416",
					height: "416",
					file: "",
				},
				this.fileList = [],
				this.$refs.UE.setContent('')
				this.imageUrl = "";
				this.isShow = false;
			},
		},
	};
</script>

<style scoped>
</style>
